<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" import="orm.*"
import="java.util.*" import="orm.Course" errorPage="errorPage.jsp" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% 	User tea  =(User) session.getAttribute("Teacher");  
	if (tea == null){
		response.sendRedirect(path + "/logout.jsp");
	}
%>
<%	String method = (String)request.getAttribute("method");
	String kid = (String)request.getAttribute("kid");
	String qid = null;
	String qtitle = null;
	String[] qoptions = null;
	String[] qkeys = null;
	String optNum = "";
	String opt = "";
	String key = "";
	int qtype = -1;
	if ("view".equals(method)){
		Question q = (Question)request.getAttribute("question");
		if (q == null){
			response.sendRedirect(path + "/logout.jsp");
		}
		else{
			qid = String.valueOf(q.getQid());
			qtype = q.getQtype().intValue();
			qtitle = "<tr style='font-size: 26px;'><td style='text-align:right'>题目：</td><td><textarea name='qTitle' "
			 	   + "id='qTitle' rows='3' cols='30' style='resize:none;font-size: 24px;'>" + q.getQtitle() + "</textarea></td></tr>";
			if (qtype != 3){
				qoptions = q.getQoption().split("\\|\\&\\&\\|");
				qkeys = q.getQkey().split("\\|\\&\\&\\|");
			 
				for (int i = 0; i < 4; i++){
			 		if (i + 2 == qoptions.length){
			 			optNum += "<option selected='selected' value='" + (i + 2) + "' >" + (i + 2) + "</option>";
			 			continue;
			 		}
			 		optNum += "<option value='" + (i + 2) + "' >" + (i + 2) + "</option>";
				}
			 
				for (int i = 0; i < qoptions.length; i++){
			 		opt += "<tr id='eachOpt'><td id='opt_td1'>选项" + (i + 1) + "：</td><td><input type='text' name='opt"
			 		 	+ (i + 1) + "' id='opt" + (i + 1) + "' value='" + qoptions[i] + "' /></td></tr>";
				}
			
				for (int i = 0; i < qoptions.length; i++){ 
					key += "<input type='";
					if(qtype == 1){ 
						key += "radio'";
					}
					else{
						key += "checkbox'";
					}
                	key += "value='" + i + "' name='keyId'";
                	for (int j = 0; j < qkeys.length; j++){ 
                		if (i == j){
                			key += "checked='checked'";
                		}
               		} 
                	key += "/>选项" + (i + 1) + "<br/>";
            	}
            }
		}
	}
	else if (!"add".equals(method)){
		response.sendRedirect(path + "/logout.jsp");
	}
%>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/tStyles.css" rel="stylesheet" type="text/css" />
<title>题目</title>
<style type="text/css">
#eachOpt {
	font-size:26px;
}
#opt_td1{
	text-align:right;
}
</style>
<script type="text/javascript">
	function changeType(type){
		var t = document.getElementById("add");
		switch (type){
		case "1":
		case "2":
			if (t.rows.length == 1){
				var str = t.innerHTML;
				var index = str.indexOf("</tbody>");
				str = str.substr(0, index);
				str += "<tr id='optNum' style='font-size: 26px;'><td style='text-align:right'>选项数：<input type='hidden'"
                			+ " value='2' id='preNum'/></td><td><select name='optionNum' id='optionNum' onchange='javascript:optNumChange(this.value);'>"
                			+ "<option selected='selected' value='2'>2</option><option value='3'>3</option><option value='4'>4</option>"
                			+ "<option value='5'>5</option></select></td></tr><tr id='eachOpt'><td id='opt_td1'>选项1：</td>"
                			+ "<td><input type='text' name='opt1' id='opt1' /></td></tr><tr id='eachOpt'><td id='opt_td1'>选项2：</td>"
                			+ "<td><input type='text' name='opt2' id='opt2' /></td></tr><tr style='font-size: 26px;'>" 
                			+ "<td style='text-align:right'>答案：</td><td style='text-align:left'>";
                if (type == "1"){
                	str += "<input type='radio' name='keyId' />选项1<br/><input type='radio' name='keyId' />选项2<br /></td></tr>";
                }
                else{
                	str += "<input type='checkbox' name='keyId' />选项1<br/><input type='checkbox' name='keyId' />选项2<br /></td></tr>";
                }
                str += "</tbody>";
                t.innerHTML = str;
			}
			else {
				var r = t.rows[t.rows.length - 1];
				var c = r.cells[1];
				var cellStr = c.innerHTML;
				if (type == "2"){
					r = /radio/g;
					cellStr = cellStr.replace(r, "checkbox");
				}
				else{
					r = /checkbox/g;
					cellStr = cellStr.replace(r, "radio");
				}
				c.innerHTML = cellStr;
			}
			break;
		case "3":
			var len = t.rows.length - 1;
			for (var i = 0; i < len; i++){
				t.deleteRow(1);
			}
			break;
		default:
			break;
		}
	}
	function optNumChange(num){
		var type = this.questionForm.qType;
		var value;
		if (type != null){
			for (var i = 0; i < type.length; i++){
				if (type[i].checked){
					value = type[i].value;
				}
			}
		}
		var t = document.getElementById("add");
		var preN = document.getElementById("preNum");
		var str = "";
		for (var i = 0; i < (parseInt(preN.value) - parseInt(num)); i++){
			var len = t.rows.length;
			t.deleteRow(len - 2);
		}
		for (var i = 0; i < (parseInt(num) - parseInt(preN.value)); i++){
			var x = t.insertRow(parseInt(preN.value) + 2 + i);
			x.id = "eachOpt";
			var c1 = x.insertCell(0);
			c1.id="opt_td1";
			c1.innerHTML = "选项" + (parseInt(preN.value) + 1 + i) + "：";
			x.insertCell(1).innerHTML = "<input type='text' name='opt" + (parseInt(preN.value) + 1 + i) 
									  + "' id='opt" + (parseInt(preN.value) + 1 + i) + "' />";
		}
		for (var i = 0; i < parseInt(num); i++){
			if (value == "1"){
				str += "<input type='radio'";
			}
			else if (value == "2"){
				str += "<input type='checkbox'";
			}
			str += "value='" + i + "' name='keyId' />选项" + (i + 1) + "<br />";
		}
		preN.value = num;
		var l = t.rows.length;
		t.rows[l - 1].cells[1].innerHTML = str;
	}
	
	function commit(str, str2, str3){
		if (check()){
			if (str.indexOf("add") != -1){
				var opt = confirm("确认添加该题目？");
				if (opt == true){
					this.questionForm.action = "manageQuestions.do?action=commitQuestion&kid=" + str2;;
					return this.questionForm.submit();
				}
				else{
					return false;
				}
			}
			else if(str.indexOf("view") != -1){
				var opt = confirm("确认修改该题目？");
				if (opt == true){
					this.questionForm.action = "manageQuestions.do?action=commitQuestion&kid=" + 
						str2 + "&qid=" + str3;
					return this.questionForm.submit();
				}
				else{
					return false;
				}
			}
			else {
				return false;
			}
		}
		return false;
	}
	function check(){
		if (this.questionForm.qTitle.value == ""){
			alert("请输入问题题目！");
			return false;
		}
		var type = this.questionForm.qType;
		var value;
		if (type != null){
			for (var i = 0; i < type.length; i++){
				if (type[i].checked){
					value = type[i].value;
				}
			}
		}
		if (value == "3"){
			return true;
		}
		var opt = new Array();
		for (var i = 0; i < parseInt(this.questionForm.optionNum.value); i++){
			var str = "opt" + (i + 1);
			if (document.getElementById(str).value == ""){
				alert("请输入所有选项！");
				return false;
			}
			var l = opt.length;
			while (l > 0){
				l--;
				if (opt[l] == document.getElementById(str).value){
					alert("选项不应相同！");
					return false;
				}
			}
			if (l == 0){
				opt.push(document.getElementById(str).value);
			}
		}
		var key = this.questionForm.keyId;
		var keyV;
		if (key != null){
			for (var i = 0; i < key.length; i++){
				if (key[i].checked){
					keyV = key[i].value;
				}
			}
		}
		if (keyV == null){
			alert("请选择答案！");
			return false;
		}
		return true;
	}
</script>
</head>

<body>
<div id = "container">
<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
            	<html:link page="/teacher/teacher.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
                <html:link page="/teacher/notice.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
                <html:link action="manageQuestions.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;题库</html:link>
                <html:link action="manageKnowledges.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;知识点</html:link>
                <html:link action="manageTemplates.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;试卷模板</html:link>
                <html:link action="gradeManagement.do?action=startMana" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;成绩管理</html:link>
                <html:link page="/teacher/studentMana.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;学生管理</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
	<!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=tea.getRealname() %>
            </div>
        	<html:link page="/teacher/teacherInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
          	<p><html:link page="/teacher/teaModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
            <p><html:link page="/teacher/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登录</html:link></p>
  </div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  <div id = "clock">
        	<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            		marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
      	<div id = "main">
        	<center>
        		<div id="question">
                	<br />
                    <br />
                	<form method="post" name="questionForm" id="questionForm" 
                		  action="" onsubmit="return commit('<%=method %>', '<%=kid %>', '<%=qid %>');">
                		<span style="font-size: 30px;"><strong>请选择题型： </strong></span><br />
                		<span id="type" style="font-size: 28px;">
                		<input type="radio" name="qType" value="1" onclick="javascript:changeType(this.value);"
                		<%if (qtype == 1 || ("add".equals(method))){ %> checked="checked" <%} %>/>单选题
                		<input type="radio" name="qType" value="2" onclick="javascript:changeType(this.value);"
                		<%if(qtype == 2){ %>checked="checked" <%} %>/>多选题
                		<input type="radio" name="qType" value="3" onclick="javascript:changeType(this.value);"
                		<%if(qtype == 3){ %>checked="checked" <%} %>/>主观题</span>
                    	<table border="1" style="text-align:center" id="add">
                    		<%if ("view".equals(method)){ %><%=qtitle %><%}else{ %>
                          	<tr style="font-size: 26px;">
                          		<td style="text-align:right">题目：</td>
                          		<td><textarea name="qTitle" id="qTitle" rows="3" cols="30" style="resize:none;font-size: 24px;"></textarea></td>
                          	</tr><%} if (qtype == 3){ %><%}else{ %>
                          	<tr id="optNum" style="font-size: 26px;">
                            	<td style="text-align:right">选项数：<input type="hidden" value="
                            	<%if("add".equals(method)){%>2<%}else{%><%=qoptions.length%><%} %>" id="preNum"/></td>
                            	<td><select name="optionNum" id="optionNum" onchange="javascript:optNumChange(this.value);">
                            		<%if("add".equals(method) || qoptions.length == 2){%>
                            		<option value="2" selected="selected">2</option>
                            		<option value="3">3</option>
                            		<option value="4">4</option>
                            		<option value="5">5</option><%}else{ %><%=optNum %><%} %>
                            	</select></td>
                          	</tr><%if ("add".equals(method)){ %>
                          	<tr id="eachOpt">
                          		<td id="opt_td1">选项1：</td>
                          		<td><input type="text" name="opt1" id="opt1" /></td>
                          	</tr>
                          	<tr id="eachOpt">
                          		<td id="opt_td1">选项2：</td>
                          		<td><input type="text" name="opt2" id="opt2" /></td>
                          	</tr><%}else{ %><%=opt %><%} %>
                          	<tr style="font-size: 26px;">
                          		<td style="text-align:right">答案：</td>
                          		<td style="text-align:left">
                          		<%if ("add".equals(method)){ %>
                          		<input type="radio" value="0" name="keyId" />选项1<br/>
                          		<input type="radio" value="1" name="keyId" />选项2<br/>
                          		<%}else{ %><%=key %><%} %></td>
                          	</tr><%} %>
                        </table>
                        <input type="submit" value="确定提交" style="font-size:18px;" />
                        &nbsp;&nbsp;&nbsp;
                        <input type="button" value="返回" style="font-size:18px;" onclick="window.location.href='manageQuestions.do?action=startManagement';"/>
                    </form>
                </div>
            </center>
   	  	</div>
		<!-- MAIN END -->
        
		<!-- FOOTER START -->
		<div id = "footer">
  	</div>
	<!-- FOOTER END -->
        
</div>
    <!-- CONTAINER END -->
</body>
</html>